﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace HYSoft.Web.Ashx
{
    /// <summary>
    /// signcheck 的摘要说明
    /// </summary>
    public class signcheck : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";

            try
            {
                //获取前台页面传递的用户名。密码，类型
                string username = context.Request["username"].ToString();
                string password = context.Request["password"].ToString();
                string type = context.Request["type"].ToString(); ;

                Model.user userModel = new Model.user();
                userModel.loginname = username;
                userModel.passwd = password;
                userModel.type = Int32.Parse(type);
                Model.user newuserModel = new BLL.user().Login(userModel);
                if (userModel.loginname == "superadmin" & userModel.type == 1 & userModel.passwd == "")
                {
                    context.Session["Username"] = userModel.loginname;
                    //
                    context.Session["uid"] = 0;
                    context.Session["uwrite"] = 1;
                    context.Session["Usertype"] ="超级用户";
                    context.Response.Write("successsuper");
                }
                else
                {
                    if (newuserModel == null)
                    {
                        context.Response.Write("fail");
                        //Response.Write("<script>alert('输入的信息有误，请重新输入！');history.back();</script>");                    
                    }
                    else
                    {
                        if (newuserModel.intvalue2 == 0)
                        {
                            context.Response.Write("wait");
                            //Response.Write("<script>alert(' [" + newuserModel.name + "]，您的注册资料等待审核，请等待或联系管理员！');history.back();;</script>");
                        }
                        else
                        {
                            context.Session["Username"] = newuserModel.name;
                            context.Session["uid"] = newuserModel.index;
                            context.Session["uwrite"] = newuserModel.intvalue1;
                            context.Session["Usertype"] = newuserModel.type == 1 ? "单位用户" : "个人用户";
                            context.Response.Write("success");
                            //Response.Write("<script>alert('欢迎 [" + newuserModel.name + "]，您已成功登陆地质灾害物联网监控系统远程控制平台！');window.location='main.aspx';</script>");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}